package com.xiaoman.read.file;

import com.xiaoman.read.config.Chapter;
import com.xiaoman.read.config.Config;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.text.PDFTextStripper;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class PdfFile implements File{
    @Override
    public void read(String url) {
        Config.setFileType("pdf");
        handlePdf(url);
    }

    private void handlePdf(String url) {
        try (PDDocument document = PDDocument.load(new java.io.File(url))) {
            PDFTextStripper pdfStripper = new PDFTextStripper();
            List<Chapter> pdfChapters = new ArrayList<>();
            Map<Integer, List<BufferedImage>> pageImages = new HashMap<>();
            int numberOfPages = document.getNumberOfPages();
            for (int i = 1; i <= numberOfPages; i++) {
                pdfStripper.setStartPage(i);
                pdfStripper.setEndPage(i);
                String pageText = pdfStripper.getText(document);
                String title = "Page " + i; // 可以根据需要自定义标题
                pdfChapters.add(new Chapter(title, pageText));
                // 提取图像
                PDPage page = document.getPage(i - 1);
                PDResources resources = page.getResources();
                List<BufferedImage> images = extractImagesFromPage(resources);
                if (!images.isEmpty()) {
                    pageImages.put(i, images);
                }
            }
            Config.chapters = pdfChapters;
            Config.pageImages = pageImages; // 存储图像
            Config.setCurrentChapterIndex(0); // 初始化当前章节索引
            displayCurrentChapter();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void displayCurrentChapter() {
        int currentIndex = Config.getCurrentChapterIndex();
        if (currentIndex >= 0) {
            if (Config.getFileType().equalsIgnoreCase("pdf") && currentIndex < Config.chapters.size()) {
                Chapter chapter = Config.chapters.get(currentIndex);
                List<BufferedImage> images = Config.pageImages.get(currentIndex + 1); // 获取当前页的图像

                // 创建一个新的面板来显示图像和文本
                JPanel panel = new JPanel();
                panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

                // 添加文本
                JTextPane textPane = new JTextPane();
                textPane.setEditable(false);
                textPane.setText("### " + chapter.title + "\n\n" + chapter.content + "\n\n");
                panel.add(textPane);

                // 添加图像
                if (images != null) {
                    for (BufferedImage image : images) {
                        ImageIcon icon = new ImageIcon(image);
                        JLabel label = new JLabel(icon);
                        panel.add(label);
                    }
                }

                // 设置内容
                Config.readUI.getTextContent().setText(""); // 清空原有内容
                Config.readUI.getScrollPane().getViewport().setView(panel); // 设置新的视图
            }
        }
    }

    private List<BufferedImage> extractImagesFromPage(PDResources resources) {
        List<BufferedImage> images = new ArrayList<>();
        try {
            for (COSName name : resources.getXObjectNames()) {
                PDXObject xObject = resources.getXObject(name);
                if (xObject instanceof PDImageXObject) {
                    PDImageXObject image = (PDImageXObject) xObject;
                    BufferedImage bim = image.getImage();
                    images.add(bim);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return images;
    }
}
